//
//  BKClassifyLeftTable.m
//  bestkeep
//
//  Created by utouu_mhm on 16/11/16.
//  Copyright © 2016年 utouu. All rights reserved.
//

#import "BKClassifyLeftTable.h"
#import "BKClasssifyLeftTableViewCell.h"
#import "masonry.h"

#define tableWidthSize 210.0f/750.0f*ScreenWidth //表格的宽度
#define tableCellHeight 50.0f //行的高度

static NSString *identifier = @"BKClasssifyLeftTableViewCell";

@interface BKClassifyLeftTable()<UITableViewDelegate,UITableViewDataSource>

@end

@implementation BKClassifyLeftTable

-(instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
    if (self = [super initWithFrame:frame style:style]) {
        self.backgroundColor = [UIColor whiteColor];
        self.showsVerticalScrollIndicator = NO;
        self.showsHorizontalScrollIndicator = NO;
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.dataSource = self;
        self.delegate = self;
        self.tableFooterView = [[UIView alloc]init];
        [self registerClass:[BKClasssifyLeftTableViewCell class] forCellReuseIdentifier:identifier];
        
        self.view = [[UIView alloc]initWithFrame:CGRectMake(tableWidthSize * 0.5 - 36, 15, 72, 20)];
        self.view.layer.cornerRadius = 10;
        self.view.layer.masksToBounds = YES;
        self.view.backgroundColor = RGBACOLOR(0, 186, 156, 1);
        self.view.hidden = YES;
        [self addSubview:self.view];
        
        self.isFirst = YES;
        self.index = 0;
    }
    return self;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return tableCellHeight;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.leftTableArray.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    BKClasssifyLeftTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    cell.classifyLeftModel = self.leftTableArray[indexPath.section];
    cell.isSeleted = (cell.classifyLeftModel == self.currentSelectModel);
    if (self.isFirst) {
        if (indexPath.section == self.index) {
            [cell setHasBeenSelected:YES];
        }else{
            [cell setHasBeenSelected:NO];
        }
    }else{
        [cell setHasBeenSelected:NO];
    }
    //修改表格行默认分隔线存空隙的问题
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        cell.layoutMargins=UIEdgeInsetsZero;
    }
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        cell.separatorInset=UIEdgeInsetsMake(0, 4, 0, 4);
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if ([self.leftTableDelegate respondsToSelector:@selector(BKClassifyLeftTable:seletedIndex:)]) {
        [self.leftTableDelegate BKClassifyLeftTable:self seletedIndex:indexPath];
    }
}

@end
